/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.venta;

import Controller.TiendaC;
import Model.MovimientoM;
import Model.TiendaM;
import Util.ArrayListTableModel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;

/**
 *
 * @author Maryluz
 */
public class frmCantidadMovimiento extends javax.swing.JDialog {

    MovimientoM oModeloM;
    int cantidad = 0;

    public frmCantidadMovimiento(java.awt.Frame parent, boolean modal, MovimientoM oM) {
        super(parent, modal);
        initComponents();
        oModeloM = oM;
        txtModelo.setText(oM.getNombrePrenda());
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        txtCantidad.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                char caracter = e.getKeyChar();

                // Verificar si la tecla pulsada no es un digito
                if (((caracter < '0')
                        || (caracter > '9'))
                        && (caracter != '\b' /*corresponde a BACK_SPACE*/)) {
                    e.consume();  // ignorar el evento de teclado
                }
            }
        });
        txtCantidad.requestFocus();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtCantidad = new javax.swing.JTextField();
        btnAgregar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtModelo = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cantidad de Envio");

        txtCantidad.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        txtCantidad.setForeground(new java.awt.Color(0, 0, 51));
        txtCantidad.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        txtCantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCantidadKeyReleased(evt);
            }
        });

        btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/flecha.png"))); // NOI18N
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(102, 0, 0));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel4.setText("Cantidad");

        jLabel1.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(102, 0, 0));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Modelo");

        txtModelo.setEditable(false);
        txtModelo.setFont(new java.awt.Font("Calibri", 1, 16)); // NOI18N
        txtModelo.setForeground(new java.awt.Color(0, 0, 102));
        txtModelo.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        txtModelo.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtCantidad, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(btnAgregar)
                        .addGap(8, 8, 8))
                    .addComponent(txtModelo))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtModelo, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                    .addComponent(btnAgregar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @SuppressWarnings("static-access")
    private void txtCantidadKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidadKeyReleased
        if (evt.getKeyCode() == evt.VK_ENTER) {
            btnAgregar.requestFocus();
        }
    }//GEN-LAST:event_txtCantidadKeyReleased

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        try {
            if (frmMovimientoAlmacen.tblEmisor.getSelectedRow() != -1) {
                @SuppressWarnings("UnusedAssignment")
                MovimientoM oM= new MovimientoM();
                oM= (MovimientoM) frmMovimientoAlmacen.tblEmisor.getValueAt(frmMovimientoAlmacen.tblEmisor.getSelectedRow(),3 );
               
                if (!buscarModelo(oM.getIdprenda()+"")) {
                    ArrayListTableModel temp = (ArrayListTableModel) frmMovimientoAlmacen.tblReceptor.getModel();
                    String m = " " + oModeloM.getNombrePrenda();
                    String id = "" + oModeloM.getIdprenda();
                    cantidad = Integer.parseInt(txtCantidad.getText());
                    
                    if (cantidad <= oModeloM.getCantidad() || frmMovimientoAlmacen.cbnEmite.getSelectedIndex() == 1) {
                        Object nuevos[] = {id, m, txtCantidad.getText(),stockTienda(id.trim(),frmMovimientoAlmacen.cbnRecepciona.getSelectedIndex()), false, oModeloM};
                        temp.addRow(nuevos);
                        this.dispose();
                    } else {
                        this.dispose();
                    }
                } else {
                    
                this.dispose();
                }
                this.dispose();
            }
            this.dispose(); 
        } catch (Exception e) {
        }
        if (frmMovimientoAlmacen.tblReceptor.getRowCount() != 0) {
            frmMovimientoAlmacen.cbnRecepciona.setEnabled(false);
        } else {
            frmMovimientoAlmacen.cbnRecepciona.setEnabled(true);
        }
        this.dispose();
    }//GEN-LAST:event_btnAgregarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtModelo;
    // End of variables declaration//GEN-END:variables

    @SuppressWarnings("CallToThreadDumpStack")
    private boolean buscarModelo(String id) {
        int filas = frmMovimientoAlmacen.tblReceptor.getRowCount();
        for (int i = 0; i < filas; i++) {
            if (id.equals(frmMovimientoAlmacen.tblReceptor.getValueAt(i, 0).toString().trim())) {
                cantidad = Integer.parseInt(txtCantidad.getText()) + (Integer.parseInt(frmMovimientoAlmacen.tblReceptor.getValueAt(i, 2).toString()));
                if (cantidad <= oModeloM.getCantidad() || frmMovimientoAlmacen.cbnEmite.getSelectedIndex() == 1) {
                    frmMovimientoAlmacen.tblReceptor.setValueAt(cantidad, i,2);
                    try {
                        int stock= stockTienda(id,frmMovimientoAlmacen.cbnRecepciona.getSelectedIndex());
                        frmMovimientoAlmacen.tblReceptor.setValueAt(stock, i,3);
                        this.dispose();
                    } catch (Exception e) {
                    e.printStackTrace();
                    }
                    
                    return true;
                } else {
                    this.dispose();
                    return true;
                }
            }
        }
        this.dispose();
        return false;
    }

    private int stockTienda(String id, int tienda) {
        
        TiendaC oC = new TiendaC();
        List<TiendaM> oMs = oC.listarStockTienda(Integer.parseInt(id),tienda );
        if (oMs != null) {
            // System.out.println("entro");
            int size = oMs.size();
            for (int a = 0; a < size; a++) {
                return oMs.get(a).getStockModelo();
            }
        }
        return 0;
    }
}
